package com.shuwen.gcdj.controller.basics;

import com.shuwen.gcdj.bean.SelectReq;
import com.shuwen.gcdj.bean.basicsMaintenance.request.MaintenanceRepairExamineReq;
import com.shuwen.gcdj.bean.basicsMaintenance.request.OperatePrestoreMaintenanceReq;
import com.shuwen.gcdj.bean.basicsMaintenance.request.OrderListReq;
import com.shuwen.gcdj.bean.basicsMaintenance.request.OrderMainSubmitReq;
import com.shuwen.gcdj.bean.basicsMaintenance.response.*;
import com.shuwen.gcdj.bean.sysDictionary.response.SysDictionaryResp;
import com.shuwen.gcdj.common.annotation.JWTAuth;
import com.shuwen.gcdj.common.api.CommonPage;
import com.shuwen.gcdj.common.api.CommonResult;
import com.shuwen.gcdj.controller.AuthController;
import com.shuwen.gcdj.dto.account.InitUserDto;
import com.shuwen.gcdj.service.basics.MaintenanceService;
import com.shuwen.gcdj.service.sysDictionary.SysDictionaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags = "维保管理")
@Slf4j
@RequestMapping
@RestController
public class MaintenanceController extends AuthController {

    @Autowired
    private MaintenanceService maintenanceService;

    @Autowired
    private SysDictionaryService sysDictionaryService;

    @ApiOperation(value = "维保列表")
    @PostMapping("/ordermaintenanceordermanagement/getmaintenanceordermanagementpage")
    @ResponseBody
    @JWTAuth
    public CommonResult<CommonPage<OrderMainResp>> getMainTen(@RequestBody OrderListReq req ) {
        return maintenanceService.getMainTen(req);
    }

    @ApiOperation(value = "维保详情")
    @PostMapping("/ordermaintenanceorderdetailmanagement/getinspectionitemlist")
    @ResponseBody
    @JWTAuth
    public CommonResult<List<OrderMainDetailsResp>> getMainTenDetails(@RequestBody SelectReq req) {
        return maintenanceService.getMainTenDetails(req.getId());
    }

    @ApiOperation(value = "维保详情-策略结构树")
    @PostMapping("/ordermaintenanceorderdetailmanagement/getinspectionitemlisttree")
    @ResponseBody
    @JWTAuth
    public CommonResult<List<OrderMainChildsResp>> getMainChirden(@RequestBody SelectReq req) {
        return maintenanceService.getMainChirden(req.getId());
    }

    @ApiOperation(value = "根据维保工单获取预存信息")
    @GetMapping("/getPrestoreMaintrnanceInfo/{orderId}")
    @ResponseBody
    @JWTAuth
    public CommonResult<List<AnomarlyStructureTreeResp>> getPrestoreMaintrnanceInfo(@PathVariable String orderId) {
        return maintenanceService.getPrestoreMaintrnanceInfo(orderId);
    }



    @ApiOperation(value = "维保详情-预保存")
    @PostMapping("/ordermaintenanceorderdetailmanagement/saveprestoremaintenanceinfo")
    @ResponseBody
    @JWTAuth
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<Integer> previewSubmit(@RequestBody OperatePrestoreMaintenanceReq req, HttpServletRequest request) {
        InitUserDto initUserDto = initUser(request);
        return maintenanceService.SavePrestoreMaintenanceInfo(req,initUserDto);
    }


    @ApiOperation(value = " 维保详情-保存")
    @PostMapping("/ordermaintenanceorderdeposited/addrangeasync")
    @ResponseBody
    @JWTAuth
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<String> submit(@RequestBody OrderMainSubmitReq req, HttpServletRequest request) {
        InitUserDto initUserDto = initUser(request);
        return maintenanceService.submit(req,initUserDto);
    }



    @ApiOperation(value = " 维保详情-修改保存")
    @PostMapping("/updataDetailRepairAsync/req")
    @ResponseBody
    @JWTAuth
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<Integer> updataDetailRepairAsync(@RequestBody MaintenanceRepairExamineReq req, HttpServletRequest request) {
        InitUserDto initUserDto = initUser(request);
        return maintenanceService.updataDetailRepairAsync(req,initUserDto);
    }


    @ApiOperation(value = " 维保详情-流程")
    @PostMapping("/ordermaintenanceorderflowmanagement/getlistasyncbyorderid")
    @ResponseBody
    @JWTAuth
    public CommonResult<List<OrderPeopleReq>> getFlow(@RequestBody SelectReq req) {
        return maintenanceService.getFlow(req.getId());
    }

    @ApiOperation(value = "获取维保等级")
    @GetMapping ("/ordermaintenanceordermanagement/getmaintenanceordertypelist")
    @ResponseBody
    @JWTAuth
    public CommonResult<List<SysDictionaryResp>> getMaintenanceOrderTypeList() {
        return CommonResult.success(sysDictionaryService.getChildByParentCode("Maintain_Type"));
    }

    @ApiOperation(value = "获取维保专业")
    @GetMapping("/ordermaintenanceordermanagement/getmaintenanceordermajorlist")
    @ResponseBody
    @JWTAuth
    public CommonResult<List<SysDictionaryResp>> getMaintenanceOrderMajorList() {
        return CommonResult.success(sysDictionaryService.getChildByParentCode("Order_Maintenanceorderdetail_Major"));
    }

    @ApiOperation(value = "获取维保状态")
    @GetMapping("/ordermaintenanceordermanagement/getmaintenanceorderstatelist")
    @ResponseBody
    @JWTAuth
    public CommonResult<List<SysDictionaryResp>> getMaintenanceOrderStateList() {
        return CommonResult.success(sysDictionaryService.getChildByParentCode("Order_Maintenanceorderdetail_State"));
    }

    @ApiOperation(value = "获取部门列表")
    @GetMapping("/basicssysdictionary/getdictionarylist")
    @ResponseBody
    @JWTAuth
    public CommonResult<List<SysDictionaryResp>> getDictionaryList() {
        return CommonResult.success(sysDictionaryService.getChildByParentCode("Basics_SysDictionary_Department"));
    }

    @ApiOperation(value = "主页-维保统计信息")
    @GetMapping("/ordermaintenanceordermanagement/getmaintenanceorderstatisticslist/{equipmentId}")
    @ResponseBody
    public CommonResult<MaintenanceStatisticsResp> getMaintenanceOrderStatisticsList(String equipmentId) {
        return CommonResult.success(maintenanceService.getMaintenanceStatistics(equipmentId));
    }
}
